py = import('python')
venv_py = join_paths(meson.project_source_root(), 'venv', 'bin', 'python3')
python3 = py.find_installation(venv_py, modules: ['mako'], required: false)
if not python3.found()
  python3 = py.find_installation('python3', modules: ['mako'], required: true)
endif

libmcpp_dep = dependency('libmcpp', fallback: ['libmcpp','libmcpp_dep'], default_options: ['tests=false'], required: true)

out_dir = meson.current_build_dir()
inc     = include_directories('.')
inc_mcpy = include_directories('../../../include')

# 让生成阶段能找到 mcpy 包
mcpy_env = environment({ 'PYTHONPATH': meson.project_source_root() })

mcpy_codegen_files = files()

# 构造命令行参数
cmd_args = [
    python3,
    '-m', 'mcpy.cli',
    '@INPUT@',
    '-o', out_dir,
    '--tag', 'module',
    '--emit-depfile', 'module.d',
    '--format', get_option('format').to_string(),
]

mod_gen = custom_target(
  'gen_network_module',
  input: files('module.py'),
  output: [ 'module.cpp', 'module.h', 'module.pyi' ],
  command: cmd_args,
  depfile: 'module.d',
  depend_files: mcpy_codegen_files,
  env: mcpy_env,
  build_by_default: true,
)

# 构造命令行参数
cmd_args = [
    python3,
    '-m', 'mcpy.cli',
    '@INPUT@',
    '-o', out_dir,
    '--tag', 'client',
    '--emit-depfile', 'client.d',
    '--format', get_option('format').to_string(),
]

client_gen = custom_target(
  'gen_network_client',
  input: files('client.py'),
  output: [ 'client.cpp', 'client.h', 'client.pyi' ],
  command: cmd_args,
  depfile: 'client.d',
  depend_files: mcpy_codegen_files,
  env: mcpy_env,
  build_by_default: true,
)

shared_library(
  'network',
  [ mod_gen[0], client_gen[0] ],
  include_directories: [inc, inc_mcpy],
  dependencies: [libmcpp_dep],
  cpp_args: ['-fPIC', '-Wall'],
)